package com.testcase;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import entity.ResultList;
import entity.ShellResult;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;

public class EvnTestDynamicTestDemo {
    @TestFactory
    Collection<DynamicTest>dynamicTestCollection(){
        return Arrays.asList(
                dynamicTest("1st dynamic test",()->{
                    assertTrue(true);
                }),
                dynamicTest("2st dynamic test",()->assertEquals(4,4))

        );
    }
    @Test
    public void entityTest() throws IOException {
        ObjectMapper objectMapper=new ObjectMapper(new YAMLFactory());
        ResultList resultList=objectMapper.readValue(new File("E:\\workspcerepeat\\junit5\\src\\main\\resources\\data\\shell_test_result.yaml"),entity.ResultList.class);
        System.out.println("done");

    }

    @TestFactory
    Collection<DynamicTest>runShellResult() throws IOException {

        List<DynamicTest> dynamicTestList = new ArrayList<>();
        ObjectMapper objectMApper = new ObjectMapper(new YAMLFactory());
        //反序列化yaml数据到对象列表中
        ResultList resultList = objectMApper.readValue(new File("E:\\workspcerepeat\\junit5\\src\\main\\resources\\data\\shell_test_result.yaml"), entity.ResultList.class);
        System.out.println("Done!");
        //动态遍历生成测试方法
        for (ShellResult shellResult: resultList.getResultList()) {
            //动态生成测试方法代码
            DynamicTest dynamicTest = dynamicTest(shellResult.getCaseName(), () -> {
                assertTrue(shellResult.isResult());
            });
            //收集操作
            dynamicTestList.add(dynamicTest);
        }
        return dynamicTestList;
    }



}
